/*
dynamic_cast.cpp:26:14: warning: ‘dynamic_cast<class Base*>(main()::C c)’ can never succeed
   26 |         pb = dynamic_cast<Base *> (&c);
      |              ^~~~~~~~~~~~~~~~~~~~~~~~~
这里仅仅只是warning

dynamic_cast failed
dynamic_cast down failed
Derived f()  //这里是碰巧执行对了，但这里的调用是有问题的；C++不检查空指针调用

 *
 */
#include <iostream>
#include <typeinfo>
using namespace std;
class Base{
	public:
		virtual ~Base(){}
};
class Derived: public Base{
	public:
		void f(){
			cout << "Derived f()" << endl;
		}
};
int  main(void){
	Base *pb, b;
	Derived *pd, d;
	class C{
		public:
			virtual void func(){}
	};
	pd = &d;
	pb = pd; //Base * <---------- Derived * 向上造型 隐式转换 编译阶段完成
	pb = dynamic_cast<Base *> (&d); //显示转换  向上造型 运行时完成的
	
	C c;
	//pb = &c; //error
	pb = dynamic_cast<Base *> (&c);
	if(pb){
		cout << "dynamic_cast ok" << endl;
	}
	else{
		cout << "dynamic_cast failed" << endl;
	}

	//向下造型
	pb = &b;
	pd = dynamic_cast<Derived *>(pb);
	if(pd){
		cout << "dynamic_cast down  ok" << endl;
	}
	else{
		cout << "dynamic_cast down failed" << endl;
	}
	pd->f();//这里的调用是有问题的
	return 0;
}

